
package pl.genapp.stocksense.libraries;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import pl.genapp.stocksense.others.PostExecuteReaction;
import android.content.Context;
import android.os.AsyncTask;
import android.provider.ContactsContract.Contacts.Data;

public class StockQuotesProvider extends AsyncTask<String, Void, HashMap<String, HashMap<String, Object>>> {
	
	private Context context;
	private PostExecuteReaction<HashMap<String, HashMap<String, Object>>> reaction;
	private String[] tagsWithOrder;
	
	/**
	 * Opisane symbole http://www.gummy-stuff.org/Yahoo-data.htm
	 * **/
	public static class SpecialTags {
		
		public static final String SYMBOL = "s";
		public static final String LAST_TRADE = "l1";
		public static final String PERCENT_CHANGE_52_WEEK_LOW = "j6";
		
	}
	
	private static class SimpleParser {
		
		static Object parseValue(String symbol, String data) {
			
			if (symbol == SpecialTags.SYMBOL) {
				
				return data.substring(1, data.length() - 1);
				
			} else if (symbol == SpecialTags.LAST_TRADE) {
				
				return Double.parseDouble(data);
				
			} else if (symbol == SpecialTags.PERCENT_CHANGE_52_WEEK_LOW) {
				
				return Double.parseDouble(data.substring(0, data.length() - 1));
				
			}
			
			return null;
			
		}
		
	}
	
	public StockQuotesProvider(Context context, PostExecuteReaction<HashMap<String, HashMap<String, Object>>> reaction) {
		this.context = context;
		this.reaction = reaction;
	}
	
	public String createLink(String entityName, String... tags) {
		
		tagsWithOrder = tags;
		
		String f = "";
		for (String s : tags) {
			f = f + s;
		}
		
		return "http://download.finance.yahoo.com/d/quotes.csv?s=" + entityName + "&f=" + f;
		
	}
	
	protected HashMap<String, HashMap<String, Object>> doInBackground(String... params) {
		
		HashMap<String, HashMap<String, Object>> result = new HashMap<String, HashMap<String, Object>>();
		
		try {
			HttpClient client = new DefaultHttpClient();
			HttpGet request = new HttpGet(params[0]);
			
			HttpResponse response = client.execute(request);
			
			BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
			
			String line;
			while ((line = rd.readLine()) != null) {
				
				String[] RowData = line.split(",");
				HashMap<String, Object> lineData = new HashMap<String, Object>();
				
				for (int i = 0; i < RowData.length; i++) {
					
					lineData.put(tagsWithOrder[i], SimpleParser.parseValue(tagsWithOrder[i], RowData[i]));
					
				}
							
				result.put(RowData[0].substring(1, RowData[0].length() - 1), lineData);
				
			}
			
			return result;
			
		} catch (IOException e) {
			
		}
		
		return null;
	}
	
	protected void onPostExecute(HashMap<String, HashMap<String, Object>> result) {
		
		if (result != null) {
			if (reaction != null) reaction.onSuccess(context, result);
		} else
			reaction.onError(context, result);
		
	}
	
}
